package cn.myjerry.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * 测试原型模式(深克隆，利用序列化和反序列化)
 * @author Jerry
 *
 */
public class Client3 {
	public static void main(String[] args) throws Exception {
		Date date = new Date(8973482795L);
		Sheep sheep = new Sheep("少丽", date);
		System.out.println(sheep.getName());
		System.out.println(sheep.getBirthday());
		System.out.println("+++++++++++++");
//		Sheep sheep1 = (Sheep) sheep.clone();
		// 利用序列化和反序列化实现深克隆
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(sheep);
		byte[] bytes = bos.toByteArray();
		
		ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
		ObjectInputStream ois = new ObjectInputStream(bis);
		Sheep sheep1 = (Sheep) ois.readObject();
		
		date.setTime(200898980958L);
		System.out.println(sheep.getBirthday());
		System.out.println("+++++++++++++++++");
		
		System.out.println(sheep1.getName());
		System.out.println(sheep1.getBirthday());
		
	}
}
